package com.example.baselib.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageInfo
import android.icu.util.ULocale.getLanguage
import java.util.*
import android.os.Build
import android.provider.Settings
import android.telephony.TelephonyManager
import android.widget.Toast
import androidx.annotation.RequiresApi
import com.example.baselib.databean.DeviceInfo
import me.jessyan.autosize.utils.LogUtils


/**
 * Author: sxygsj
 * Date: 2020/4/15 14:34
 * Description: 系统工具类
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
object EquipmentUtil {

    /**
     * 获取当前手机系统语言。
     * @return 返回当前系统语言。例如：当前设置的是“中文-中国”，则返回“zh-CN”
     */
    private fun getSystemLanguage(): String {
        return Locale.getDefault().getLanguage()
    }

    /**
     * 获取当前系统上的语言列表(Locale列表)
     * @return  语言列表
     */
    fun getSystemLanguageList(): Array<Locale> {
        return Locale.getAvailableLocales()
    }

    /**
     * 获取当前手机系统版本号
     * @return  系统版本号
     */
    private fun getSystemVersion(): String {
        return android.os.Build.VERSION.RELEASE
    }

    /**
     * 获取手机型号
     * @return  手机型号
     */
    fun getSystemModel(): String {
        return android.os.Build.MODEL
    }

    /**
     * 获取手机设备名
     * @return  手机设备名
     */
    private fun getSystemDevice(): String {
        return Build.DEVICE
    }

    /**
     * 获取手机厂商
     * @return  手机厂商
     */
    private fun getDeviceBrand(): String {
        return android.os.Build.BRAND
    }

    /**
     * 获取手机主板名
     * @return  主板名
     */
    private fun getDeviceBoard(): String {
        return Build.BOARD
    }

    /**
     * 获取手机厂商名
     * @return  手机厂商名
     */
    private fun getDeviceManufacturer(): String {
        return Build.MANUFACTURER
    }

    /**
     * 获取Android_id
     * @return  Android_id
     */
    private fun getAndroidId(context: Context): String {
        return Settings.System.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
    }

    /**
     * 获取程序信息
     * @return PackageInfo
     */
    private fun getPackageInfo(context: Context): PackageInfo {
        return context.packageManager.getPackageInfo(context.packageName, 0)
    }

    /**
     * 获取程序信息
     * @return PackageInfo
     */
    @SuppressLint("MissingPermission")
    private fun getSimInfo(deviceInfo: DeviceInfo, context: Context) {
        val telephonyManager =
            context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            deviceInfo.simSerial = ""
            deviceInfo.imei = ""
            deviceInfo.imsi = ""
        } else {
            deviceInfo.imei = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                telephonyManager.imei else telephonyManager.deviceId
            deviceInfo.imsi =
                if (telephonyManager.subscriberId == null) "" else telephonyManager.subscriberId
            deviceInfo.simSerial =
                if (telephonyManager.simSerialNumber == null) "" else telephonyManager.simSerialNumber
        }
        deviceInfo.phoneNumber =
            if (telephonyManager.line1Number == null) "" else telephonyManager.line1Number
        deviceInfo.countryIso =
            if (telephonyManager.networkCountryIso == null) "" else telephonyManager.networkCountryIso
    }



    fun getDeviceInfo(context: Context): DeviceInfo {
        val deviceInfo = DeviceInfo()
        try {
            getSimInfo(deviceInfo, context)
//            deviceInfo.androidId = getAndroidId(context)
            deviceInfo.androidId="db4f6f45200d76e2"
            deviceInfo.deviceBoard = getDeviceBoard()
            deviceInfo.deviceManufacturer = getDeviceManufacturer()
            deviceInfo.systemDevice = getSystemDevice()
            deviceInfo.systemModel = getSystemModel()
            deviceInfo.systemVersion = getSystemVersion()
            deviceInfo.appVersionCode =if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
                getPackageInfo(context).longVersionCode.toString() else getPackageInfo(context).versionCode.toString()
            deviceInfo.appVersionName = getPackageInfo(context).versionName

//            simInfo.voiceMail = telephonyManager.voiceMailNumber
//            simInfo.carrier = telephonyManager.networkOperatorName
//            simInfo.simOperator = telephonyManager.simOperator
//            simInfo.phoneType = telephonyManager.phoneType
//            simInfo.radioType = telephonyManager.networkType

        } catch (e: Exception) {
            Toast.makeText(context,e.message,Toast.LENGTH_SHORT).show()
        }
        return deviceInfo
    }



}